#define LIBTELNET_IMPLEMENTATION
#include "libtelnet.h"
#include <stdio.h>

void handle_event(telnet_session* session, const telnet_event* event) {
    switch (event->type) {
        case TELNET_EVENT_CONNECTED:
            printf("已连接到服务器\n");
            break;
            
        case TELNET_EVENT_DATA:
            printf("收到数据: %.*s", (int)event->event.data.length, event->event.data.data);
            break;
            
        case TELNET_EVENT_DISCONNECTED:
            printf("与服务器断开连接\n");
            break;
            
        case TELNET_EVENT_ERROR:
            printf("错误: %s\n", event->event.error.message);
            break;
    }
}

int main() {
    // 创建会话
    telnet_session* session = telnet_create(4096);
    if (!session) {
        printf("无法创建会话\n");
        return 1;
    }
    
    // 连接到服务器
    if (telnet_connect(session, "example.com", 23) != 0) {
        printf("连接失败\n");
        telnet_destroy(session);
        return 1;
    }
    
    // 处理事件循环
    while (session->state == TELNET_STATE_CONNECTED) {
        telnet_process(session, handle_event);
        
        // 检查用户输入并发送
        char buffer[1024];
        if (fgets(buffer, sizeof(buffer), stdin)) {
            telnet_send(session, buffer, strlen(buffer));
        }
    }
    
    // 清理
    telnet_destroy(session);
    return 0;
}
